<?php
namespace backend\models\activeRecords;

use Yii;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%spread_url}}".
 *
 * @property string $id 自增id
 * @property int $game_id 游戏ID
 * @property string $game_url 游戏推广链接
 */
class SpreadUrl extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%spread_url}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            [['game_id','game_url'], 'required'],
            [['game_id'], 'integer'],
            [['game_id'], 'unique','message'=>'该游戏已创建链接'],
            [['game_url'], 'string', 'max' => 500],
            [['game_url'], 'validateUrl'],
        ];
    }

    public function scenarios()
    {
        $config =  parent::scenarios(); // TODO: Change the autogenerated stub
        $config['default'] = ['game_id','game_url'];
        $config['create'] = ['game_id','game_url'];
        $config['update'] = ['game_url'];
        return $config;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', '自增id'),
            'game_id' => Yii::t('app', '游戏ID'),
            'game_url' => Yii::t('app', '游戏推广链接'),
        ];
    }

    public function getGame(){
        return $this->hasOne(TGame::className(),['game_id'=>'game_id']);
    }

    public function validateUrl($attribute ,$params){
        if(!$this->hasErrors()){
            $result = filter_var($this->game_url, FILTER_VALIDATE_URL);
            if(!$result){
                $this->addError($attribute,'请输入正确的链接');
            }
        }
    }


}
